﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyCancer.Framework.Communication.Interfaces;
using CrazyCancer.Framework.ShortUrl.Interfaces;
using System.Net;
using CrazyCancer.Framework.Communication.Enums;
using System.IO;
using System.Xml;

namespace CrazyCancer.Framework.ShortUrl
{
    public class BitLy : IUrlShortener
    {
        #region IUrlShortener Members

        public string ShortenUrl(string longUrl, string apiUrl, ICommunicator communicator)
        {
            throw new NotImplementedException();
        }


        public string ShortenUrl(string longUrl, string apiUrl, ICommunicator communicator, string userName, string password)
        {
            string urlToSend = string.Format(apiUrl, longUrl, userName, password);
            WebResponse webResponse = communicator.SendRequest(urlToSend, RequestMethod.GET);

            StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()); 
            String response = responseStream.ReadToEnd();
            responseStream.Close();

            BitLyResponse bitLyResponse = new BitLyResponse(response);

            return bitLyResponse.Results.NodeKeyVal.ShortUrl;
        }



        #endregion
    }

    public class BitLyResponse
    {
        public int ErrorCode { get; internal set; }
        public string ErrorMessage { get; internal set; }
        public BitLyResults Results { get; internal set; }
        public string StatusCode { get; internal set; }

        public BitLyResponse(string xmlString)
        {
            Load(xmlString);
        }

        public void Load(string xmlString)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlString);

            //Refactor this with XPath
            XmlNodeList nodes = document.GetElementsByTagName("errorCode");
            XmlNode node = nodes[0];

            string text = node.InnerText;

            int errorCode = 0;
            if(int.TryParse(text, out errorCode))
            {
                ErrorCode = errorCode;
            }

            nodes = document.GetElementsByTagName("errorMessage");
            node = nodes[0];
            ErrorMessage = node.InnerText;

            nodes = document.GetElementsByTagName("results");
            node = nodes[0];

            Results = new BitLyResults(node);

            nodes = document.GetElementsByTagName("statusCode");
            node = nodes[0];
            StatusCode = node.InnerText;
        }
    }

    public class BitLyResults
    {
        public BitLyNodeKeyVal NodeKeyVal;

        public BitLyResults(XmlNode node)
        {
            Load(node);
        }

        public void Load(XmlNode node)
        {
            XmlNode child = node.FirstChild;
            NodeKeyVal = new BitLyNodeKeyVal(child);
        }
    }

    public class BitLyNodeKeyVal
    {
        public string ShortUrl { get; internal set; }

        public BitLyNodeKeyVal(XmlNode node)
        {
            Load(node);
        }

        public void Load(XmlNode node)
        {
            XmlNode child = node.SelectSingleNode("//shortUrl");
            ShortUrl = child.InnerText;
        }
    }
}
